//给定一个二叉树，找出其最大深度。 
//
// 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。 
//
// 说明: 叶子节点是指没有子节点的节点。 
//
// 示例： 给定二叉树 [3,9,20,null,null,15,7]， 
//
//     3
//   / \
//  9  20
//    /  \
//   15   7 
//
// 返回它的最大深度 3 。 
//
// Related Topics 树 深度优先搜索 广度优先搜索 二叉树 👍 1628 👎 0

package leetcode.editor.cn;

import java.util.*;

//java:二叉树的最大深度
public class Q0104MaximumDepthOfBinaryTree {
    public static void main(String[] args) {
//        Solution solution = new Q0104MaximumDepthOfBinaryTree().new Solution();
        TreeNode root = new TreeNode(5);
        root.left = new TreeNode(4);
        root.right = new TreeNode(8);
        root.left.left = new TreeNode(11);
        root.left.left.left = new TreeNode(7);
        root.left.left.right = new TreeNode(2);
        root.right.left = new TreeNode(13);
        root.right.right = new TreeNode(4);
        root.right.right.left = new TreeNode(5);
        root.right.right.right = new TreeNode(1);
        int maxDepth = new Q0104MaximumDepthOfBinaryTree().new Solution().maxDepth(root);
        System.out.println(maxDepth);
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     * int val;
     * TreeNode left;
     * TreeNode right;
     * TreeNode() {}
     * TreeNode(int val) { this.val = val; }
     * TreeNode(int val, TreeNode left, TreeNode right) {
     * this.val = val;
     * this.left = left;
     * this.right = right;
     * }
     * }
     */
    class Solution {
        public int maxDepth(TreeNode root) {
            // dfs
//        if (root == null) return 0;
//        int maxDepthOfLeft = maxDepth(root.left);
//        int maxDepthOfRight = maxDepth(root.right);
//        return Math.max(maxDepthOfLeft, maxDepthOfRight) + 1;
            // BFS
            if (root == null) return 0;
            Queue<TreeNode> queue = new LinkedList<>();
            List<Integer> valueList = new ArrayList<>();
            int layer = 0;
            queue.offer(root);
            while (!queue.isEmpty()) {
                Queue<TreeNode> queueTemp = new LinkedList<>();
                for (TreeNode cur : queue) {
                    TreeNode temp = queue.poll();
                    valueList.add(temp.val);
                    if (temp.left != null) queueTemp.offer(temp.left);
                    if (temp.right != null) queueTemp.offer(temp.right);
                }
                queue = queueTemp;
                layer++;
            }
            return layer;

        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}